package com.hzhjhh.view.manageView.menus.accountMng.accountQuery;

import java.util.Objects;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.constant.DateFormatContants;
import com.hzhjhh.entity.UserInfo;
import com.hzhjhh.mapper.UserInfoMapper;
import com.hzhjhh.means.Select_Means;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.accountMng.AccountQueryDialog;

import cn.hutool.core.date.DateUtil;

/**
 * 菜单栏初始化-仓库-账户管理-个人账户查询弹出框-查询按钮
 * 
 * @author 12083
 *
 */
public class QueryBt {

	private static UserInfoMapper userInfoMapper;
	
	/**
	 * 按钮
	 */
	public static JButton btInit(AccountQueryDialog accountQueryDialog) {
		JButton query = new JButton();
		query.setBounds(108, 317, 83, 25);
		query.setText("查询");

		query.addActionListener(event -> {
			JTextField txfCardId = accountQueryDialog.getTxfCardId();
			String cardId = txfCardId.getText(); // 卡号
//			Vector Select_vector = new Vector();
//			Select_vector = Select_Means.Select_UserManager(cardId); // 查询此卡是否存在
			UserInfo userInfo = userInfoMapper.selectByCardId(cardId);
			if (Objects.isNull(userInfo)) {
				JOptionPane.showMessageDialog(accountQueryDialog, "卡号不存在", "提示", JOptionPane.INFORMATION_MESSAGE);
				return;
			}

			txfCardId.setText(userInfo.getCardId().toString()); // 卡号
			accountQueryDialog.getTxfCardOwner().setText(userInfo.getUserName()); // 持卡人
			accountQueryDialog.getTxfIDCard().setText(userInfo.getUserId()); // 身份证
			accountQueryDialog.getTxfPassword().setText(userInfo.getUserPassword()); // 密码
			accountQueryDialog.getTxfCardOpenDate().setText(DateUtil.format(userInfo.getCardDate(), DateFormatContants.YYYY_MM_DD_HH_MM_SS)); // 开卡日期
			String userType = userInfo.getUserType();
			JComboBox<String> cobUserType = accountQueryDialog.getCobUserType();
			if (StringUtils.equalsAny(userType, "普通会员", "收银员")) {
				cobUserType.removeAllItems();
				cobUserType.addItem("普通会员");
				cobUserType.addItem("收银员");
			}
//			if (Select_vector.get(5).toString().equals("普通会员")) {
//				cobUserType.removeAllItems();
//				cobUserType.addItem("普通会员");
//				cobUserType.addItem("收银员");
//
//			}
//			if (Select_vector.get(5).toString().equals("收银员")) {
//				cobUserType.removeAllItems();
//				cobUserType.addItem("收银员");
//				cobUserType.addItem("普通会员");
//
//			}
			accountQueryDialog.getTxfTerm().setText(userInfo.getTerm()); // 期限
			accountQueryDialog.getTxfScore().setText(userInfo.getScore().toString()); // 积分
			accountQueryDialog.getTxfDiscount().setText(userInfo.getDiscount().toString()); // 折扣
			accountQueryDialog.getTxaRemark().setText(userInfo.getRemark()); // 备注
		});
		
		// 数据库操作对象初始化
		SqlSession sqlSession = DBUtils.getSqlSession(true);
		userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
		
		return query;
	}
}
